﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class DataProvider
    {
        /// <summary>
        /// Tạo kết nối đến CSDL QLCHH tren sql cua moi may
        /// </summary>
        /// <returns></returns>
        public static SqlConnection ConnectDB()
        {
            //string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=QLCHH;Integrated Security=True";
            string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=db_quanlyhoa;Integrated Security=True";
            SqlConnection conn;
            conn = new SqlConnection(connectionString);
            conn.Open();
            return conn;
        }


        SqlDataAdapter da;
        DataTable dt;
        public static SqlConnection Con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=db_quanlyhoa;Integrated Security=True");
        private SqlCommand cmd;
        public void connect_h()
        {
            if (Con.State == ConnectionState.Closed)
            {
                Con.Open();
            }
        }
        public void DisConnect_h()
        {
            if (Con.State == ConnectionState.Open)
            {
                Con.Close();
            }
        }
        //getdata
        public DataTable GetData(string sql)
        {
            connect_h();
            cmd = new SqlCommand(sql, Con);
            da = new SqlDataAdapter(cmd);
            dt = new DataTable();
            da.Fill(dt);
            DisConnect_h();
            return dt;
        }
        //cute
        public void ExeCuteNonQuery(string sql)
        {
            connect_h();
            cmd = new SqlCommand(sql, Con);
            cmd.ExecuteNonQuery();
            DisConnect_h();
        }
        //doc thong tin tu csdl
        public SqlDataReader dataReader(string sql)
        {
            connect_h();
            cmd = new SqlCommand(sql, Con);
            SqlDataReader dr = cmd.ExecuteReader();
            return dr;
        }
    }
}
